﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WoSai.Base.Common;

namespace WoSai.Base.Config
{
    public class BaseConfigFileManager
    {
        private static string configFilePath;
        private static IConfigInfo configInfo;
        private static object locker = new object();
        /// <summary>
        /// 配置文件所在路径
        /// </summary>
        public static string ConfigFilePath
        {
            get { return BaseConfigFileManager.configFilePath; }
            set { BaseConfigFileManager.configFilePath = value; }
        }
        /// <summary>
        /// 配置对象
        /// </summary>
        public static IConfigInfo ConfigInfo
        {
            get { return BaseConfigFileManager.configInfo; }
            set { BaseConfigFileManager.configInfo = value; }
        }
        /// <summary>
        /// 锁对象
        /// </summary>
        public static object Locker
        {
            get { return BaseConfigFileManager.locker; }
            set { BaseConfigFileManager.locker = value; }
        }
        protected static IConfigInfo LoadConfig(ref DateTime filechangetime, string configFilePath, IConfigInfo configInfo)
        {
            return LoadConfig(ref filechangetime, configFilePath, configInfo, true);
        }
        /// <summary>
        /// 加载配置文件
        /// </summary>
        /// <param name="filechangetime">文件修改时间</param>
        /// <param name="configFilePath">文件路径</param>
        /// <param name="configInfo">配置文件类的类型</param>
        /// <param name="check">是否检测文件是否最新,当false时,重新读取config文件</param>
        /// <returns></returns>
        protected static IConfigInfo LoadConfig(ref DateTime filechangetime, string configFilePath, IConfigInfo configInfo,bool check)
        {
            BaseConfigFileManager.configFilePath = configFilePath;
            BaseConfigFileManager.configInfo = configInfo;
            if (check)
            {
                DateTime filenewchange = System.IO.File.GetLastWriteTime(configFilePath);

                if (filechangetime != filenewchange)
                {
                    filechangetime = filenewchange;
                    lock (locker)
                    {
                        BaseConfigFileManager.configInfo = DeserializeConfig(configFilePath, configInfo.GetType());
                    }
                }
            }
            else
            {
                lock (locker)
                {
                    BaseConfigFileManager.configInfo = DeserializeConfig(configFilePath, configInfo.GetType());
                }
            }
            return BaseConfigFileManager.configInfo;
        }
        /// <summary>
        /// 反序列化config类
        /// </summary>
        /// <param name="configFilePath">文件路径</param>
        /// <param name="configType">文件类型</param>
        /// <returns></returns>
        public static IConfigInfo DeserializeConfig(string configFilePath, Type configType)
        {
            return (IConfigInfo)SerializationHelper.Load(configFilePath, configType);
        }
        public bool SaveConfig(string configFilePath, IConfigInfo configInfo)
        {
            return SerializationHelper.Save(configInfo, configFilePath);
        }
    }
}
